<?php
/**
 * 春哥团队（www.cgtblog.com） - 春哥多端多商户万能DIY商城系统
 * Copyright © 2024 春哥技术团队 保留所有权利
 * =========================================================

 * ----------------------------------------------
 * 您只能在商业授权范围内使用，不可二次转售、分发、分享、传播
 * 任何企业和个人不得对代码以任何目的任何形式的再发布
 * =========================================================
 */

namespace app\controller;
use think\facade\Db;
class ApiMendian extends ApiCommon{
    public function mendianlist(){
        $bid = input('param.bid/d',0);
        $proid = input('param.proid','');//多个用逗号分隔，售卖某商品的店
        $longitude = input('param.longitude','');
        $latitude = input('param.latitude','');
        $pernum = 10;
        $pagenum = input('param.pagenum');
        if(!$pagenum) $pagenum = 1;
        $mdwhere = [];
        $mdwhere[] = ['aid','=',aid];
        $mdwhere[] = ['status','=',1];
        $field = '*';
        //如果一个商品包含全部，则为全部门店
        if($proid){
            $productlist = Db::name('shop_product')->where('id','in',explode(',',$proid))->field('id,bind_mendian_ids')->select()->toArray();
            $tmpmdids = [];
            if(empty($productlist)) $productlist = [];
            foreach ($productlist as $k=>$product){
                $bindMendianIds = $product['bind_mendian_ids']?explode(',',$product['bind_mendian_ids']):[];
                if(empty($bindMendianIds) || in_array('-1',$bindMendianIds)){
                    break;
                }
                $tmpmdids = array_merge($tmpmdids,$bindMendianIds);
            }
            if($tmpmdids){
                $mdwhere[] = ['id','in',array_unique($tmpmdids)];
            }
        }else{
		    $mdwhere[] = ['bid','=',$bid];
		}

        if($longitude && $latitude){
            $field .= ",round(6378.138*2*asin(sqrt(pow(sin( ({$latitude}*pi()/180-latitude*pi()/180)/2),2)+cos({$latitude}*pi()/180)*cos(latitude*pi()/180)* pow(sin( ({$longitude}*pi()/180-longitude*pi()/180)/2),2)))*1000) AS distance";
            $mdorder = Db::raw("({$longitude}-longitude)*({$longitude}-longitude) + ({$latitude}-latitude)*({$latitude}-latitude) asc");
        }else{
            $field .= ",0 distance";
            $mdorder = 'sort desc,id asc';
        }
        $mendianlist = Db::name('mendian')->field($field)->where($mdwhere)->orderRaw($mdorder)->page($pagenum,$pernum)->select()->toArray();
        if(empty($mendianlist)){
            $mendianlist = [];
        }
        foreach ($mendianlist as $mdkey=>$mendian){
            if(empty($mendian['distance'])){
                $mendianlist[$mdkey]['distance'] = '';
            }elseif($mendian['distance']<1000){
                $mendianlist[$mdkey]['distance'] = round($mendian['distance'],1).'m';
            }else{
                $mendianlist[$mdkey]['distance'] = round($mendian['distance']/1000,1).'km';
            }
            $mendianlist[$mdkey]['distanceNumKm'] = $mendian['distance'] ? round($mendian['distance']/1000,1) : 0;
            $mendianlist[$mdkey]['distanceNumM'] = $mendian['distance'] ? $mendian['distance'] : 0;
            $mendianlist[$mdkey]['address'] = $mendian['address']??'';
            $mendianlist[$mdkey]['area'] = $mendian['area']??'';
        }
        return $this->json(['status'=>1,'msg'=>'','data'=>$mendianlist]);
    }
    //默认门店
    public function getNearByMendian(){
        $mendian_id = input('param.mendian_id/d',0);
        $bid = input('param.bid',0);
        $mendian_isinit = input('param.mendian_isinit');
        $latitude = input('param.latitude','');
        $longitude = input('param.longitude','');
        $bfield = 'id,name,province,city,district,address,longitude,latitude';
        if($mendian_id && !$mendian_isinit){
            //不是初始化的用户选择的门店
            $mendian = Db::name('mendian')->where('id',$mendian_id)->field($bfield)->find();
        }else if($latitude && $longitude){
            $mdorder = Db::raw("({$longitude}-longitude)*({$longitude}-longitude) + ({$latitude}-latitude)*({$latitude}-latitude) asc");
            $mendian = Db::name('mendian')->where('aid',aid)->where('status',1)->where('bid',$bid)->orderRaw($mdorder)->field($bfield)->find();
        }else{
            $mendian = Db::name('mendian')->where('aid',aid)->where('status',1)->where('bid',$bid)->order('sort desc,id asc')->field($bfield)->find();
        }
        if($mendian){
            $mendian['address'] = ($mendian['province']??'').($mendian['city']??'').($mendian['address']??'');
            $bdistance = '';
            if($mendian['latitude'] && $mendian['longitude'] && $latitude && $longitude){
                $bdistance = getdistance($longitude,$latitude,$mendian['longitude'],$mendian['latitude'],2);
            }
            $mendian['distance'] = $bdistance?$bdistance.'km':'';
            return $this->json(['status'=>1,'msg'=>'','mendian'=>$mendian??'']);
        }else{
            return $this->json(['status'=>0,'msg'=>'','msg'=>'未查询到门店']);
        }
    }
    
    public function getMendianCategory(){
        }
}